﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

namespace Jumpton.SFA.Server.BusinessRule
{
    public class SIMInfoManager : IBusinessLogic
    {
        #region IBusinessLogic Members

        object IBusinessLogic.Load(decimal ID)
        {
            throw new NotImplementedException();
        }

        object IBusinessLogic.LoadByCode(string code)
        {
            return ObjectRow.Load(typeof(SIMInfo), "IMSICode='" + code + "'");
        }

        object IBusinessLogic.LoadWithCondition(string Condition)
        {
            return new ObjectTable(typeof(SIMInfo), Condition);
        }

        void IBusinessLogic.Save(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion

        public SIMInfo LoadByCode(String code)
        {
            return (SIMInfo)((IBusinessLogic)this).LoadByCode(code);
        }

        public ObjectTable LoadWithCondition(String condition)
        {
            return new ObjectTable();
            //return (ObjectTable)((IBusinessLogic)this).LoadWithCondition(condition);
        }

        /// <summary>
        /// 快速检索功能
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ObjectTable QuickSearch(String condition)
        {
            //String sql = "isActive=1 and id not in (select SimId from UserDeviceBound)";

            //if (!condition.Equals(""))
            //{
            //    sql += " and (IMSICode like '{0}' or PhoneNumber like '{0}')";
            //}


            string sql = "select 0;";
            return LoadWithCondition(sql);
        }

        /// <summary>
        /// 判断一个SIM信息是否已经存在
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Boolean IsExists(String IMSICode, String phoneNumber)
        {
            String condition = "";
            if (!IMSICode.Equals(""))
            {
                condition += String.Format("IMSICode='{0}'",IMSICode);
            }

            if(!phoneNumber.Equals(""))
            {
                if(!condition.Equals(""))
                {
                    condition += " or ";
                }

                condition += String.Format("PhoneNumber='{0}'",phoneNumber);
            }

            // 判断当前输入的IMSI是否已经存在
            ObjectTable exists = LoadWithCondition(condition);
            return exists.Count > 0;
        }
    }
}
